import Vue from "vue";
import vuex from "vuex"

import axios from "axios"
axios.defaults.baseURL = "https://autumnfish.cn"
Vue.use(vuex)
const actions = {
    // 搜索
    async usInputComplete({ commit }, songsName) {
        const data = await axios({
            url: "/search",
            params: { keywords: songsName }
        })

        const { request } = data
        if (request.status >= 200 && request.status < 300) {
            if (request.readyState === 4) {
                const { songs } = data.data.result
                commit("USINPUTCOMPLETE", songs)
            }
        }
    },

    async getItemSong({ commit }, aSong) {
        const data = await axios({
            url: "/song/detail",
            params: { ids: aSong }
        })

        const { request } = data
        if (request.status >= 200 && request.status < 300) {
            if (request.readyState === 4) {
                const { songs } = data.data
                commit("GETITEMSONG", songs[0])
            }
        }
    },

    async getMusic({ commit }, songId) {
        const data = await axios({
            url: "/song/url",
            params: { id: songId }
        })

        const { request } = data
        if (request.status >= 200 && request.status < 300) {
            if (request.readyState === 4) {
                const music = data.data.data[0]
                commit("GETMUSIC", music)
            }
        }
    }
}
const mutations = {
    // 搜索
    USINPUTCOMPLETE(state, songs) {
        state.songs = songs
    },
    //获取用户点击歌曲的id
    GETUSERCLICKSONGID(state, userClickSongId) {
        state.user.clickSongId = userClickSongId
    },
}
const state = {
    songs: [],
    user: {
        clickSongId: ""
    }
}
const getters = {}

export default new vuex.Store({
    actions, mutations, state, getters
})
